#include <stdio.h>
#include <stdlib.h>

#define MaxSize 5//注意，最后没有分号

/**
 * 栈的顺序存储实现
 * 栈的特点：先进后出
 * */
typedef struct SNode *Stack;
struct SNode{
    int Data[MaxSize];
    int Top;//栈顶元素的下标
};

struct SNode S;
Stack Ptrs;

//压栈
void push(Stack Ptrs,int item);
//弹栈
int pop(Stack Ptrs);
void show(Stack Ptrs);
Stack create();


int main(){
    Stack s = create();
    push(s,1);
    push(s,2);
    // pop(s);
    show(s);
    return 0;
}

Stack create(){
    Stack s = (Stack)malloc(sizeof(struct SNode));
    for(int j=0;j<MaxSize;j++){
        s->Data[j] = 0;
    }
    s->Top = -1;
    return s;
}

void push(Stack Ptrs,int item){
    if(Ptrs->Top == MaxSize-1){
        //如果是链式存储就不需要判断是否已满
        printf("堆栈已满");
        return;
    }else{
        //先top+1,再赋值
        Ptrs->Data[++(Ptrs->Top)] = item;
        return;
    }
}

int pop(Stack Ptrs){
    if(Ptrs->Top == -1){
        printf("堆栈为空");
        return -1;
    }else{
        //先返回，然后top-1
        return (Ptrs->Data[(Ptrs->Top)--]);
    }
}

void show(Stack Ptrs){
    Stack s = Ptrs;
    printf("stack:");
    for(int i = 0;i<=s->Top;i++){
        printf(" %d ", s->Data[i]);
    }
    printf("\n");
}